package homework.score;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ObjectStore<T> {

    private static final Map<Class, ObjectStore> instances = new HashMap<Class, ObjectStore>();

    private Map<String, T> objects = new HashMap<String, T>();

    private ObjectStore() {}

    public T setObject(String key, T s) {
        return objects.put(key, s);
    }

    public T getObject(String key) {
        return objects.get(key);
    }

    public T removeObject(String key) {
        return objects.remove(key);
    }

    public Collection<T> getObjects() {
        return objects.values();
    }

    public Set<Map.Entry<String, T>> getEntries() {
        return objects.entrySet();
    }

    public static <T> ObjectStore<T> getInstance(Class<T> clazz) {

        ObjectStore store = instances.get(clazz);

        if (store == null) {
            store = new ObjectStore();
            instances.put(clazz, store);
        }

        return store;
    }
}
